/*
 * Swarm.java
 *
 * Created on May 26, 2007, 10:55 AM
 *
 */

package pso;

/**
 *
 * @author Greg Robinson gregarobi@dev.java.net
 */
public abstract class Swarm implements SwarmInterface {
    
    protected Particle[] _particles;
    protected double _social;
    protected double _cognitive;
    
    protected double _bestFit;
    protected double[] _bestPos;
    
    /** Creates a new instance of Swarm */
    public Swarm() {
    }
    
    public abstract boolean better(double current, double previous);
    
    public boolean better(double current) {
        return better(current, _bestFit);
    }
    
    public double[] findBestPos() {
        double fitness;
        for(int i=0; i<_particles.length; i++) {
            Particle p = _particles[i];
            fitness = p.getBestFit();
            if(better(fitness)) {
                _bestPos = p.getBest();
                _bestFit = p.getBestFit();
            }
        }
        
        return _bestPos;
    }
    
    public void update() {
        for(int i=0; i<_particles.length; i++) {
            Particle p = _particles[i];
            p.update(_bestPos);
        }
    }
    
}
